﻿using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;

namespace BaseProject.Infrastructure.Extensions
{
    public static class RedisExtensions
    {
        public static void SetRedis<T>(this IDistributedCache cache, string key, T value)
        {
            var val = JsonConvert.SerializeObject(value).GetByte();
            cache.Set(key, val);
        }

        public static void SetRedis<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options)
        {
            var val = JsonConvert.SerializeObject(value).GetByte();
            cache.Set(key, val, options);
        }

        public static async Task SetRedisAsync<T>(this IDistributedCache cache, string key, T value)
        {
            var val = JsonConvert.SerializeObject(value).GetByte();
            await cache.SetAsync(key, val);
        }

        public static async Task SetRedisAsync<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options)
        {
            var val = JsonConvert.SerializeObject(value).GetByte();
            await cache.SetAsync(key, val, options);
        }

        public static void RefreshRedis<T>(this IDistributedCache cache, string key, T value)
        {
            cache.Remove(key);
            SetRedis<T>(cache, key, value);
        }

        public static void RefreshRedis<T>(this IDistributedCache cache, string key, T value, Action<T> action)
        {
            cache.Remove(key);
            action(value);
            SetRedis<T>(cache, key, value);
        }

        public static void RefreshRedis<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options)
        {
            cache.Remove(key);
            SetRedis<T>(cache, key, value, options);
        }

        public static async Task RefreshRedisAsync<T>(this IDistributedCache cache, string key, T value)
        {
            await cache.RemoveAsync(key);
            await SetRedisAsync<T>(cache, key, value);
        }

        public static async Task RefreshRedisAsync<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options)
        {
            await cache.RemoveAsync(key);
            await SetRedisAsync<T>(cache, key, value, options);
        }

        public static T GetRedis<T>(this IDistributedCache cache, string key)
        {
            return JsonConvert.DeserializeObject<T>(cache.Get(key).GetString());
        }

        public static async Task<T> GetRedisAsync<T>(this IDistributedCache cache, string key)
        {
            return JsonConvert.DeserializeObject<T>((await cache.GetAsync(key)).GetString());
        }

        public static byte[] GetByte(this string source)
        {
            return Encoding.UTF8.GetBytes(source);
        }

        public static string GetString(this byte[] source)
        {
            return Encoding.UTF8.GetString(source);
        }
    }
}